﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors.User
{
    public partial class editSale : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ckeditorEditSaleDescription.Width = 450;
                this.Form.DefaultButton = btnEditSale.UniqueID;

                PopulateCategoryDropDown();
                BindData();
                BindSubMenu();

                lblConfirmUpdatedSale.Visible = false;

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Binds the sub menu links.
        /// </summary>
        private void BindSubMenu()
        {
            hyLinkProfile.NavigateUrl = "~/User/profile.aspx?uid=" + Request.QueryString["uid"];
            hyLinkEditProfile.NavigateUrl = "~/User/editProfile.aspx?uid=" + Request.QueryString["uid"];
            hyLinkChangePassword.NavigateUrl = "~/User/changePassword.aspx?uid=" + Request.QueryString["uid"];
            hyLinkSettings.NavigateUrl = "~/User/editSettings.aspx?uid=" + Request.QueryString["uid"];
            hyLinkSale.NavigateUrl = "~/User/userSale.aspx?uid=" + Request.QueryString["uid"];
            hyLinkPurchases.NavigateUrl = "~/User/userPurchase.aspx?uid=" + Request.QueryString["uid"];
        }

        /// <summary>
        /// Populates the Category DropDownList.
        /// </summary>
        private void PopulateCategoryDropDown()
        {
            List<Category> categories = dbExchange.GetAllCategories();

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (categories.Count > 0)
            {
                ddlEditSaleCategory.DataSource = categories;

                if (!culture.Equals("en-GB"))
                    ddlEditSaleCategory.DataTextField = "CategoryName";
                else
                    ddlEditSaleCategory.DataTextField = "LocalizedCategoryName";

                ddlEditSaleCategory.DataValueField = "CategoryID";
                ddlEditSaleCategory.DataBind();
            }
        }

        /// <summary>
        /// Edits a sale based on the input parameters.
        /// </summary>
        /// <param name="title">Specifies the title to be used in the edit.</param>
        /// <param name="description">Specifies the description to be used in the edit.</param>
        /// <param name="categoryId">Specifies the ID of the category to be used in the edit.</param>
        /// <param name="isActive">Specifies whether or not the sale should be active or not.</param>
        /// <param name="saleId">Specifies the ID of the sale to be edited.</param>
        private void EditSale(string title, string description, int categoryId, bool isActive, int saleId)
        {
            dbProfile.UpdateUserSale(title, description, categoryId, isActive, saleId);
        }

        /// <summary>
        /// Binds the data of a sale, based on the "sid" QueryString.
        /// </summary>
        private void BindData()
        {
            Sale sal = dbExchange.GetSaleDetails(Convert.ToInt32(Request.QueryString["sid"]));

            ddlEditSaleCategory.SelectedValue = sal.Category.CategoryName;
            chkBoxEditSaleStatus.Checked = sal.IsActive;

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (!String.IsNullOrEmpty(culture))
                chkBoxEditSaleStatus.Text = (chkBoxEditSaleStatus.Checked) ? "Active" : "Inactive";
            else
                chkBoxEditSaleStatus.Text = (chkBoxEditSaleStatus.Checked) ? "Aktiv" : "Inaktiv";
            
            txtEditSaleTitle.Text = sal.Title;
            ckeditorEditSaleDescription.Text = sal.Description;
        }

        protected void btnEditSale_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                lblConfirmUpdatedSale.Visible = true;
                string title = txtEditSaleTitle.Text;
                string description = ckeditorEditSaleDescription.Text;
                int categoryId = Convert.ToInt32(ddlEditSaleCategory.SelectedValue);
                bool isActive = chkBoxEditSaleStatus.Checked;
                int saleId = Convert.ToInt32(Request.QueryString["sid"]);

                EditSale(title, description, categoryId, isActive, saleId);
            }
        }

        protected void btnEditSaleCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/User/userSale.aspx?uid=" + Membership.GetUser().ProviderUserKey);
        }

        //Event fired when the Checked property of the checkbox is changed
        protected void chkBoxEditSaleStatus_CheckedChanged(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["MyCulture"].ToString() == "en-GB")
                chkBoxEditSaleStatus.Text = (chkBoxEditSaleStatus.Checked) ? "Active" : "Inactive";
            else
                chkBoxEditSaleStatus.Text = (chkBoxEditSaleStatus.Checked) ? "Aktiv" : "Inaktiv";
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            btnEditSale.Text = GetLocalResourceObject("btnEditSaleText").ToString();
            btnEditSaleCancel.Text = GetLocalResourceObject("btnEditSaleCancelText").ToString();

            hyLinkChangePassword.Text = GetLocalResourceObject("hyLinkChangePasswordText").ToString();
            hyLinkEditProfile.Text = GetLocalResourceObject("hyLinkEditProfileText").ToString();
            hyLinkProfile.Text = GetLocalResourceObject("hyLinkProfileText").ToString();
            hyLinkPurchases.Text = GetLocalResourceObject("hyLinkPurchasesText").ToString();
            hyLinkSale.Text = GetLocalResourceObject("hyLinkSaleText").ToString();
            hyLinkSettings.Text = GetLocalResourceObject("hyLinkSettingsText").ToString();

            lblEditSaleCategory.Text = GetLocalResourceObject("lblEditSaleCategoryText").ToString();
            lblEditSaleDescription.Text = GetLocalResourceObject("lblEditSaleDescriptionText").ToString();
            lblEditSaleHeader.Text = GetLocalResourceObject("lblEditSaleHeaderText").ToString();
            lblEditSaleStatus.Text = GetLocalResourceObject("lblEditSaleStatusText").ToString();
            lblEditSaleTitle.Text = GetLocalResourceObject("lblEditSaleTitleText").ToString();
            lblConfirmUpdatedSale.Text = GetLocalResourceObject("lblConfirmUpdatedSaleText").ToString();

            string descriptionValidation = GetLocalResourceObject("rfvalEditSaleDescriptionText").ToString();
            rfvalEditSaleDescription.ToolTip = descriptionValidation;
            rfvalEditSaleDescription.ErrorMessage = descriptionValidation;

            string titleValidation = GetLocalResourceObject("rfvalEditSaleTitleText").ToString();
            rfvalEditSaleTitle.ToolTip = titleValidation;
            rfvalEditSaleTitle.ErrorMessage = titleValidation;

            string categoryValidation = GetLocalResourceObject("rfvalEditSaleCategoryText").ToString();
            rfvalEditSaleCategory.ToolTip = categoryValidation;
            rfvalEditSaleCategory.ErrorMessage = categoryValidation;

            valsumEditSale.HeaderText = GetLocalResourceObject("valsumEditSaleText").ToString();

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (culture.Equals("en-GB"))
                ckeditorEditSaleDescription.Language = "en";
            else
                ckeditorEditSaleDescription.Language = "da";
        }
    }
}